package com.zmn.oms.dubbo.impl.factory;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.common.dio.factory.order.FactoryOrderWorkAddDIO;
import com.zmn.oms.common.dio.normal.orderadd.OrderProductDIO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.dto.zmn.order.NewOrderDRO;
import com.zmn.oms.dubbo.interfaces.factory.FactoryOrderWorkModifyRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.bo.order.OrderBO;
import com.zmn.oms.model.dto.factory.OrderFactoryDTO;
import com.zmn.oms.model.dto.order.zmn.FactoryNewOrderDTO;
import com.zmn.oms.model.dto.order.zmn.FactoryNewOrderPartDIO;
import com.zmn.oms.model.entity.post.OrderPost;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.zmn.business.interfaces.work.autoflow.WorkFlowContextBService;
import com.zmn.oms.zmn.normal.business.interfaces.order.FactoryOrderBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 类描述：
 *
 * @author wangxiaokun
 * @since 2021/04/06 10:18
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 10000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class FactoryOrderWorkModifyRemoteServiceImpl implements FactoryOrderWorkModifyRemoteService {
    private static final String TAG = "新厂商单";

    @Autowired
    private FactoryOrderBService factoryOrderBService;
    @Autowired
    private WorkFlowContextBService workFlowContextBService;

    @Override
    public ResponseDTO<NewOrderDRO> addOrder(FactoryOrderWorkAddDIO factoryOrderAddDIO) {
        log.info("[{}] 添加订单：{}", TAG, JSON.toJSONString(factoryOrderAddDIO));

        try {
            // DIO转换
            // 基础信息
            FactoryNewOrderDTO newOrderDTO = BeanMapper.map(factoryOrderAddDIO, FactoryNewOrderDTO.class);
            BeanMapper.copy(factoryOrderAddDIO.getOrderWorkBaseDIO(), newOrderDTO);
            newOrderDTO.setIp(factoryOrderAddDIO.getOperatorIP());
            // 用户信息
            BeanMapper.copy(factoryOrderAddDIO.getUserDIO(), newOrderDTO);
            newOrderDTO.setUserName(StringUtils.defaultString(factoryOrderAddDIO.getUserDIO().getContactName(), factoryOrderAddDIO.getUserDIO().getUserName()));
            // 厂商信息
            OrderFactoryDTO factoryDTO = new OrderFactoryDTO();
            OrderProductDIO orderProductDIO = factoryOrderAddDIO.getOrderProductDIO();
            factoryDTO.setFactoryId(factoryOrderAddDIO.getFactoryId());
            factoryDTO.setProductId(orderProductDIO.getProductId());
            factoryDTO.setFcProductNumber(orderProductDIO.getNumber());
            factoryDTO.setFaultIdList(orderProductDIO.getFaultIdList());
            factoryDTO.setRemark(orderProductDIO.getRemark());
            newOrderDTO.setOrderFactory(factoryDTO);
            // 快递信息
            List<OrderPost> orderPostList = BeanMapper.mapList(factoryOrderAddDIO.getOrderPostList(), OrderPost.class);
            newOrderDTO.setOrderPostList(orderPostList);
            // 厂商配件信息
            List<FactoryNewOrderPartDIO> orderPartPostList = BeanMapper.mapList(factoryOrderAddDIO.getOrderPartPostList(), FactoryNewOrderPartDIO.class);
            newOrderDTO.setOrderPartPostList(orderPartPostList);
            OrderBO orderBO = factoryOrderBService.insertOrder(newOrderDTO);

            OrderWork orderWork = orderBO.getOrderWork();
            NewOrderDRO newOrderDRO = new NewOrderDRO();
            newOrderDRO.setWorkId(orderWork.getWorkId());
            newOrderDRO.setOrderId(orderWork.getOrderId());
            newOrderDRO.setUserId(orderWork.getUserId());
            log.info("[{}] 添加订单返回：{}", TAG, newOrderDRO);

            // 工单自动流转
            workFlowContextBService.asyncAutoFlow(orderWork.getOrderId(), orderWork.getWorkId());
            return ResponseDTO.success(newOrderDRO, "OK");

        } catch (OmsBaseException ex) {
            return ResponseDTO.fail(ex.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            log.error("[{}] 订单保存错误：{}", TAG, e);
            return ResponseDTO.fail("添加订单失败");
        }
    }
}
